package template;

import callback.ResultSetExtractor;
import callback.RowMapper;
import exception.DataAccessException;
import util.JdbcUtil;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JdbcTemplate {


    public void update(String sql,Object... args){
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con= JdbcUtil.getConnection();

            ps=con.prepareStatement(sql);

            if(args!=null) {
                for (int i = 0; i < args.length; i++) {
                    setParam(i + 1, args[i], ps);
                }
            }

            ps.executeUpdate();


        }catch (Exception e){
            e.printStackTrace();
            throw new DataAccessException("数据访问失败",e);
        }finally{
            JdbcUtil.close(null,ps,con);
        }
    }

    public <T> T query(String sql, ResultSetExtractor<T> rse, Object... args){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        T result=null;
        try{
            con=JdbcUtil.getConnection();

            ps=con.prepareStatement(sql);

            if(args!=null){
                for (int i = 0; i < args.length; i++) {
                    setParam(i+1,args[i],ps);
                }
            }

            rs=ps.executeQuery();

            result=rse.extractData(rs);

        }catch (Exception e){
            e.printStackTrace();
            throw new DataAccessException("数据访问失败",e);
        }finally{
            JdbcUtil.close(rs,ps,con);
        }
        return result;

    }

    public <T> List<T> query(String sql, RowMapper<T> rm, Object... args){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<T> list=new ArrayList<>();
        try{
            con=JdbcUtil.getConnection();

            ps=con.prepareStatement(sql);

            if(args!=null){
                for (int i = 0; i < args.length; i++) {
                    setParam(i+1,args[i],ps);
                }
            }

            rs=ps.executeQuery();

            while(rs.next()){
                T t=rm.mapRow(rs);
                list.add(t);
            }


        }catch (Exception e){
            e.printStackTrace();
            throw new DataAccessException("数据访问失败",e);
        }finally{
            JdbcUtil.close(rs,ps,con);
        }
        return list;
    }























    private void setParam(int index,Object param,PreparedStatement ps) throws Exception{
        if(param==null){
            ps.setNull(index, Types.NULL);
        }else if(param instanceof String){
            ps.setString(index, (String) param);
        }else if(param instanceof Integer||param.getClass().equals(int.class)){
            ps.setInt(index, (Integer) param);
        }else if(param instanceof Long||param.getClass().equals(long.class)){
            ps.setLong(index, (Long) param);
        }else if(param instanceof Double||param.getClass().equals(double.class)){
            ps.setDouble(index, (Double) param);
        }else if(param instanceof BigDecimal){
            ps.setBigDecimal(index, (BigDecimal) param);
        }else if(param instanceof Boolean||param.getClass().equals(boolean.class)){
            ps.setBoolean(index, (Boolean) param);
        }else if(param instanceof java.sql.Date){
            ps.setDate(index, (java.sql.Date) param);
        }else if(param instanceof java.sql.Time){
            ps.setTime(index, (java.sql.Time) param);
        }else if(param instanceof java.sql.Timestamp){
            ps.setTimestamp(index, (java.sql.Timestamp) param);
        }else if(param instanceof Date){
            ps.setTimestamp(index,new Timestamp(((Date)param).getTime()));
        }else{
            ps.setObject(index,param);
        }
    }
}


























